<script setup>
import { ref,reactive } from 'vue'
import { userGetMenu,userSetmenu,menuList } from '../../../api/index.js'
import { onMounted } from 'vue'
import { nextTick } from 'vue'
import { Plus } from '@element-plus/icons-vue'
import { useRoute } from 'vue-router'

const route = useRoute() 

onMounted(() => {
    userGetMenu().then(({data}) => {
        permissionData.value = data.data
    })
    getListData()
})
const paginationData = reactive({
    pageNum: 1,
    pageSize: 10
})
const handleSizeChange = (val) => {
    paginationData.pageSize = val
    getListData()
}
const handleCurrentChange = (val) => {
    paginationData.pageNum = val
    getListData()
}
//列表数据
const tableData = reactive({
    list:[],
    total:0
})
//打开弹窗
const open = (rowData = {}) => {
    dialogFormVisable.value = true
    //弹窗打开和form生成是异步
    nextTick(() => {
        //浅拷贝
        if(rowData){
            Object.assign(form,{id:rowData.id, name:rowData.name})
            treeRef.value.setCheckedKeys(rowData.permission)
        }
    })
}
//请求列表的数据
const getListData = () => {
    menuList(paginationData).then(({data})=>{
        console.log(data)
        const {list,total} = data.data
        tableData.list = list
        tableData.total = total
    })
}
//弹窗的显示隐藏
const dialogFormVisable = ref(false)
//关闭弹窗的回调
const beforeClose = () => {
    dialogFormVisable.value = false
    //重置表单
    formRef.value.resetFields()
    //tree选择重置
    treeRef.value.setCheckedKeys(defaultKeys)
}
const form = reactive({
    id:'',
    name:'',
    permissions:''
})
//权限树形结构菜单
const permissionData = ref([]) 
//选中权限
const defaultKeys = [4,5]
const treeRef = ref()
//创建表单的实例
const formRef = ref()
//表单校验
const rules = reactive({
    name:[{required:true,trigger:'blur',message:'请输入权限名称'}]
})
//表单提交
const confirm = async (formEl) => {
    if (!formEl) return
    //获取成功之后运行validate方法，手动触发校验
    await formEl.validate((valid, fields) => {
        if (valid) {
            //获取到选中的checkbox数据
            const permissions = JSON.stringify(treeRef.value.getCheckedKeys())
            userSetmenu({
                name:form.name,
                permissions:permissions,
                id:form.id
            }).then(({data})=>{
                //关闭弹窗
                beforeClose()
                //请求列表
                getListData()
            })
        } else {
          console.log('error submit!', fields)
        }
    })
}
</script>

<template>
    <panel-head :route="route"></panel-head>
    <div class="btns">
        <el-button :icon="Plus" type="primary" @click="open(null)" size="small">新建</el-button>
    </div>
    <!--v-model用来控制弹窗是否打开-->
    <el-table :data="tableData.list" style="width: 100%">
        <el-table-column prop="id" label="id"/>
        <el-table-column prop="name" label="昵称"/>
        <el-table-column prop="permissionName" label="菜单权限" width="500px"/>
        <el-table-column label="操作"> 
            <template #default="scope">
                <el-button type="primary" @click="open(scope.row)">编辑</el-button>
            </template>
        </el-table-column>
    </el-table>
    <div class="pagination-info">
        <el-pagination
            v-model:current-page="paginationData.pageNum"
            :page-size="paginationData.pageSize"
            :background="false"
            size="small"
            layout="total, prev, pager, next"
            :total="tableData.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
        />
    </div>
    <el-dialog 
        v-model="dialogFormVisable" 
        title="添加权限" 
        width="500"
        :before-close="beforeClose">
        <el-form
            ref="formRef"
            label-width="100px"
            label-position="left"
            :model="form"
            :rules="rules"
        >
            <el-form-item v-show="false" prop="id"> 
                <el-input v-model="form.id"></el-input>
            </el-form-item>
            <el-form-item label="名称" prop="name">
                <el-input v-model="form.name" placeholder="请填写权限名称"></el-input>
            </el-form-item>
            <el-form-item label="权限" prop="permission">
                <el-tree 
                    ref="treeRef"
                    style="max-width:600px" 
                    :data="permissionData"
                    node-key="id"
                    show-checkbox
                    :default-checked-keys="defaultKeys"
                    :default-expanded-keys="[2]"
                ></el-tree>
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button type="primary" @click="confirm(formRef)">确认</el-button>
            </div>
        </template>
    </el-dialog>
</template>

<style scoped>
.btns{
    padding: 10px 0 10px 10px;
    background-color: #fff;
}
</style>